package com.m7788.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.imagemanager.util.CacheService;
import com.m7788.screen.R;
import com.m7788.util.AppUtil;
import com.m7788.util.Constant;
import com.tool.media.Image;

public class LettersAdapter extends BaseAdapter {

	private ArrayList<Image> list;
	private LayoutInflater inflater;
	private Context context;

	public LettersAdapter(Context context, ArrayList<Image> items) {
		this.list = items;
		this.context = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Image getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	static class ListItemView {
		TextView path;
		TextView num;
		ImageView iv;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ListItemView listItemView = null;

		if (null == convertView) {
			convertView = inflater.inflate(R.layout.adapter_listview_letter,
					null);
			listItemView = new ListItemView();
			listItemView.path = (TextView) convertView
					.findViewById(R.id.all_tv_path);
			listItemView.num = (TextView) convertView
					.findViewById(R.id.all_tv_num);
			listItemView.iv = (ImageView) convertView.findViewById(R.id.all_iv);
			// 设置控件到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}

		Image info = list.get(position);
		if (null != info) {
			// listItemView.path.setText(info.getTitle());
			listItemView.path.setText(info.getPath().substring(0,
					info.getPath().lastIndexOf("/")));
			listItemView.num.setText("图片数量：" + info.getPhotoNum());
			if (info.isCheck()) {
				listItemView.iv.setBackgroundResource(R.drawable.grid_check_on);
			} else {
				listItemView.iv
						.setBackgroundResource(R.drawable.grid_check_off);
			}
		}
		return convertView;
	}

	public void addItem(Image info) {
		list.add(info);
	}

	public void changeCheck(Image info) {
		if (null != list && list.size() > 0) {
			info.setCheck(!info.isCheck());

			for (int i = 0; i < list.size(); i++) {
				if (info.getTitle().equals(list.get(i).getTitle())) {
					list.set(i, info);
					break;
				}
			}
			changeConfig(info);
		}
	}

	public void clearList() {
		if (null != list && list.size() > 0) {
			for (int i = list.size() - 1; i >= 0; i--) {
				list.remove(i);
			}
		}
	}

	private void changeConfig(Image info) {
		String config = AppUtil.getRecord(context, Constant.DIRECTORYCONFIG);
		String p = info.getPath().substring(0, info.getPath().lastIndexOf("/"));
		if (info.isCheck()) {
			if (config.indexOf(p+":") > 0) {

			} else {
				CacheService.addDcimFolder(p);
				//初始化startScreen页面用到
				AppUtil.saveRecord(context, Constant.DIRECTORYCONFIG, config
						+ ";" + p+":");
			}
		} else {
			if (config.indexOf(p+":") > 0) {
				config = config.replace(";" + p+":", "");
				AppUtil.saveRecord(context, Constant.DIRECTORYCONFIG, config);
				CacheService.delDcimFolder(p);
			}
		}
	}

}
